//
//  YZMStockDetailViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/29.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockDetailViewController.h"
#import "YZMCombLabelView.h"
#import "YZClient.h"
#import "YZMTradeSettlementModel.h"
#import "YZMTradeSTDetailModel.h"
#import "YZMResultView.h"
#import "NSNumber+Utils.h"
#import "UILabel+Color.h"
#import "Masonry+Utils.h"
#import "YZMWebViewService.h"
#import "MBProgressHUD+Utils.h"
#import <DateTools/DateTools.h>
#import <AFNetworking/AFNetworkReachabilityManager.h>

@interface YZMStockDetailViewController()

@property (nonatomic, strong) YZMTradeSettlementModel *trade;
@property (nonatomic, strong) YZMTradeSTDetailModel *tradeDetail;
@property (nonatomic, strong) YZMResultView *resultView;
@property (nonatomic, assign) YZM_TRADE_TYPE tradeType;

@end

@implementation YZMStockDetailViewController

- (void)viewDidLoad{
    
    [super viewDidLoad];
    self.title = @"策略详情";
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    self.resultView = [YZMResultView new];
    self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
    __weak __typeof(&*self) weakSelf = self;
    self.resultView.retryAction = ^{
        if (weakSelf.tradeType==YZM_TRADE_TYPE_FOR_A_STOCK_SIM) {
            [weakSelf fetchStockDetailForSimulate];
        }else if (weakSelf.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE){
            [weakSelf fetchStockDetailForFreeEx];
        }else if (weakSelf.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_YLB){
            [weakSelf fetchStockYlbDetail];
        }else{
            [weakSelf fetchStockDetail];
        }
    };
    [self.view addSubview:self.resultView];
    [self.resultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
                                                 name:AFNetworkingReachabilityDidChangeNotification object:nil];
    if (self.tradeType==YZM_TRADE_TYPE_FOR_A_STOCK_SIM) {
        [self fetchStockDetailForSimulate];
    }else if(self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE){
        [self fetchStockDetailForFreeEx];
    }else if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_YLB){
        [self fetchStockYlbDetail];
    }else{
        [self fetchStockDetail];
    }
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -- network

- (void)reachabilityChanged:(NSNotification *)note{
    if (!kNetworkNotReachability) {
        if (self.tradeType==YZM_TRADE_TYPE_FOR_A_STOCK_SIM) {
            [self fetchStockDetailForSimulate];
        }else if(self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE){
            [self fetchStockDetailForFreeEx];
        }else{
            [self fetchStockDetail];
        }
    }
}

- (void)fetchStockDetail{
    
    [YZClient td_fetchTradeSettlementDetailWithID:self.trade.tid success:^(NSURLResponse *response, id responseObject) {
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
            self.tradeDetail = topModel.data;
            [self createObjects];
            
        }else{
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }
        
    } failure:^(NSError *error) {
       
        [self.resultView syncStatusWithError:error];
    }];
}

- (void)fetchStockYlbDetail{
    
    [YZClient td_fetchTradeYlbSettlementDetailWithID:self.trade.tid success:^(NSURLResponse *response, id responseObject) {
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
            self.tradeDetail = topModel.data;
            [self createObjects];
            
        }else{
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }
        
    } failure:^(NSError *error) {
        
        [self.resultView syncStatusWithError:error];
    }];
}

- (void)fetchStockDetailForFreeEx{
    
    [YZClient td_fetchTradeFreeExSettlementDetailWithID:self.trade.strategyId success:^(NSURLResponse *response, id responseObject) {
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
            self.tradeDetail = topModel.data;
            [self createObjects];
            
        }else{
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }
        
    } failure:^(NSError *error) {
        
        [self.resultView syncStatusWithError:error];
    }];
}


-(void)fetchStockDetailForSimulate{
    [YZClient simU_fetchTradeSettlementDetailWithID:self.trade.tid success:^(NSURLResponse *response, id responseObject) {
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
            self.tradeDetail = topModel.data;
            [self createObjects];
            
        }else{
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }
        
    } failure:^(NSError *error) {
        
        [self.resultView syncStatusWithError:error];
    }];
}

#pragma mark -- event

- (void)clickToShowAgreement:(UIButton *)sender{
    
    if (!YZMUserServiceInstance.isValidate) {
        
        [MBProgressHUD showMessage:@"网络恢复中，请稍后重试"];
        return;
    }
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
//        [YZMWebViewService openCooperationDelegateWithTradeID:self.tradeDetail.strategyId withType:self.tradeType];
    }else{
         [YZMWebViewService openCooperationDelegateWithTradeID:self.tradeDetail.tid withType:self.tradeType];
    }
   
}

#pragma mark -- ui

- (void)createObjects{
    
    UIScrollView *sv = [UIScrollView new];
    [self.view addSubview:sv];
    [sv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    UIView *containerView = [UIView new];
    [sv addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.width.equalTo(sv);
    }];
    
    UIView *lastView = nil;
    NSArray *array = @[[self firstPartView],[self secondPartView],[self thirdPartView],[self fourthPartView],[self fifthPartView]];
    for (UIView *partView in array) {
        
        [containerView addSubview:partView];
        [partView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.equalTo(containerView);
            if (lastView) {
                
                make.top.equalTo(lastView.mas_bottom);
            }else{
                
                make.top.equalTo(containerView);
            }
        }];
        lastView = partView;
    }
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView);
    }];
}

- (UIView *)firstPartView{
    
    UIView *customView = [UIView new];
    
    UILabel *label = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForMainTextColor]];
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        label.text = [NSString stringWithFormat:@"JQ%@",@(self.trade.strategyId)];
    }else{
        label.text = [NSString stringWithFormat:@"JQ%@",@(self.trade.tid)];
    }
    [customView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(customView).offset(15);
        make.bottom.equalTo(customView).offset(-15);
    }];
    
    UIButton *agreementBT = [UIButton new];
    [agreementBT setTitle:@"  合约协议  " forState:UIControlStateNormal];
    [agreementBT setTitleColor:[YZMAppStyle colorForBlueText] forState:UIControlStateNormal];
    [agreementBT.titleLabel setFont:[YZMAppStyle smallSizeFont]];
    [agreementBT addTarget:self action:@selector(clickToShowAgreement:) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:agreementBT];
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        agreementBT.hidden = YES;
    }
    [agreementBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(customView);
        make.left.equalTo(label.mas_right).offset(4);
    }];
    if (self.tradeType==YZM_TRADE_TYPE_FOR_A_STOCK_ONE || self.tradeType==YZM_TRADE_TYPE_FOR_A_STOCK_SIM) {
        agreementBT.hidden=YES;
    }
    
    return [self unitViewWithString:@"策略交易号" andCustomView:customView];
}

- (UIView *)secondPartView{
    
    UIView *customView = [UIView new];
    
    UILabel *leftFirstLB = [YZMAppStyle labelWithSmallLightGray];
    leftFirstLB.text = @"投资人";
    [customView addSubview:leftFirstLB];
    [leftFirstLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(customView).offset(15);
    }];
    
    UILabel *rightFirstLB = [YZMAppStyle labelWithSmallWhite];
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
         rightFirstLB.text = [self.tradeDetail.investorUserName stringByAppendingFormat:@"(ID:%@)",@(self.tradeDetail.investorUserId)];
    }else{
         rightFirstLB.text = [self.tradeDetail.investorUsername stringByAppendingFormat:@"(ID:%@)",@(self.tradeDetail.investorUserId)];
    }
    [customView addSubview:rightFirstLB];
    [rightFirstLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftFirstLB);
        make.left.equalTo(leftFirstLB.mas_right).offset(10);
    }];
    
    UILabel *leftSecondLB = [YZMAppStyle labelWithSmallLightGray];
    leftSecondLB.text = @"点买人";
    [customView addSubview:leftSecondLB];
    [leftSecondLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftFirstLB.mas_bottom).offset(10);
        make.left.equalTo(leftFirstLB);
        make.bottom.equalTo(customView).offset(-15);
    }];
    
    UILabel *rightSecondLB = [YZMAppStyle labelWithSmallWhite];
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
         rightSecondLB.text = [self.tradeDetail.userName stringByAppendingFormat:@"(ID:%@)",@(self.tradeDetail.userId)];
    }else{
        rightSecondLB.text = [self.tradeDetail.username stringByAppendingFormat:@"(ID:%@)",@(self.tradeDetail.userId)];
    }
    [customView addSubview:rightSecondLB];
    [rightSecondLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftSecondLB);
        make.left.equalTo(rightFirstLB);
    }];
    
    return [self unitViewWithString:@"交易合作" andCustomView:customView];
    
}

- (UIView *)thirdPartView{
    
    UIView *customView = [UIView new];
    
    UIView *leftStackView;
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
      leftStackView  = [self gridStackViewWithArray:@[@"股票名称",@"买入价格",@"买入类型",@"买入时间",@"交易数量",@"综合费"] andTextColor:[YZMAppStyle colorForLightGrayText]];
    }else{
          leftStackView  = [self gridStackViewWithArray:@[@"股票名称",@"买入价格",@"买入类型",@"买入时间",@"持仓天数",@"综合费"] andTextColor:[YZMAppStyle colorForLightGrayText]];
    }
    [customView addSubview:leftStackView];
    [leftStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(customView).offset(15);
        make.bottom.equalTo(customView).offset(-15);
    }];
    UIView *firstStackView;
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        firstStackView = [self gridStackViewWithArray:@[self.tradeDetail.stockName,self.tradeDetail.realBuyPrice.p02fString,@"即时成交",[self.tradeDetail.realBuyTime formattedDateWithFormat:@"YYYY-MM-dd"],[NSString stringWithFormat:@"%ld股",self.tradeDetail.holdNum],@(self.tradeDetail.serviceCharge).p02fString] andTextColor:[YZMAppStyle colorForMainTextColor]];
    }else{
        firstStackView = [self gridStackViewWithArray:@[self.tradeDetail.stockName,@(self.tradeDetail.buyPriceDeal).p02fString,@"即时成交",[self.tradeDetail.dealTime formattedDateWithFormat:@"YYYY-MM-dd"],[NSString stringWithFormat:@"%@个交易日", @(self.tradeDetail.holdDays)],@(self.tradeDetail.serviceCharge).p02fString] andTextColor:[YZMAppStyle colorForMainTextColor]];
    }
    [customView addSubview:firstStackView];
    [firstStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftStackView.mas_right).offset(10);
        make.top.equalTo(leftStackView);
        
    }];
    
    NSArray *valueArray;
    switch (self.tradeType) {
        case YZM_TRADE_TYPE_FOR_A_STOCK:
        case YZM_TRADE_TYPE_FOR_A_STOCK_YLB:
        case YZM_TRADE_TYPE_FOR_A_STOCK_SIM:
            valueArray=@[@(self.tradeDetail.money).p02fString,@(self.tradeDetail.sellPriceDeal).p02fString,@"即时成交",[self.tradeDetail.closeTime formattedDateWithFormat:@"YYYY-MM-dd"],[NSString stringWithFormat:@"%@股",@(self.tradeDetail.volumeDeal)],[@(self.tradeDetail.deferCharge).p02fString stringByAppendingFormat:@"(%@次)",@(self.tradeDetail.holdDays-2)]];
                if (self.tradeDetail.holdDays<=2) {
            valueArray=@[@(self.tradeDetail.money).p02fString,@(self.tradeDetail.sellPriceDeal).p02fString,@"即时成交",[self.tradeDetail.closeTime formattedDateWithFormat:@"YYYY-MM-dd"],[NSString stringWithFormat:@"%@股",@(self.tradeDetail.volumeDeal)],@(self.tradeDetail.deferCharge).p02fString];
                }
            
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_D:
        case YZM_TRADE_TYPE_FOR_A_STOCK_ONE:
            valueArray=@[@(self.tradeDetail.money).p02fString,@(self.tradeDetail.sellPriceDeal).p02fString,@"即时成交",[self.tradeDetail.closeTime formattedDateWithFormat:@"YYYY-MM-dd"],[NSString stringWithFormat:@"%@股",@(self.tradeDetail.volumeDeal)],@""];
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_FREE:
      valueArray=@[self.tradeDetail.holdvalue.p02fString,self.tradeDetail.realSellPrice.p02fString,@"即时成交",[self.tradeDetail.liquidateTime formattedDateWithFormat:@"YYYY-MM-dd"],@"",@""];
            break;
            
            
        default:
            break;
    }
    
    UIView *rightStackView = [self gridStackViewWithArray:valueArray andTextColor:[YZMAppStyle colorForMainTextColor]];
    [customView addSubview:rightStackView];
    [rightStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customView).offset(15);
        make.right.equalTo(customView).offset(-15);
    }];
    NSArray *nameArray;

    switch (self.tradeType) {
        case YZM_TRADE_TYPE_FOR_A_STOCK:
        case YZM_TRADE_TYPE_FOR_A_STOCK_YLB:
        case YZM_TRADE_TYPE_FOR_A_STOCK_SIM:
            nameArray=@[@"交易本金",@"卖出价格",@"卖出类型",@"卖出时间",@"交易数量",@"递延费"];
            break;
            
            case YZM_TRADE_TYPE_FOR_A_STOCK_D:
            nameArray=@[@"交易本金",@"卖出价格",@"卖出类型",@"卖出时间",@"交易数量",@""];
            break;
            case YZM_TRADE_TYPE_FOR_A_STOCK_ONE:
            case YZM_TRADE_TYPE_FOR_A_STOCK_FREE:
            nameArray=@[@"交易本金",@"卖出价格",@"卖出类型",@"卖出时间",@"",@""];
            break;
            
            
        default:
            break;
    }
    
    UIView *secondStackView = [self gridStackViewWithArray:nameArray andTextColor:[YZMAppStyle colorForLightGrayText]];
    [customView addSubview:secondStackView];
    [secondStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rightStackView);
        make.right.equalTo(rightStackView.mas_left).offset(-10);
    }];
    
    UIView *line = [secondStackView viewWithTag:99];
    line.backgroundColor = [YZMAppStyle colorForSplitLine];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(customView).offset(15);
        make.right.equalTo(customView).offset(-15);
        make.height.mas_equalTo(0.5);
    }];
    
    return [self unitViewWithString:@"交易明细" andCustomView:customView];
}

- (UIView *)gridStackViewWithArray:(NSArray *)array andTextColor:(UIColor *)textColor{
    
    UIView *stackView = [UIView new];
    
    UIView *lastView = nil;
    for (NSString *content in array) {
        
        NSInteger index = [array indexOfObject:content];
        if (index == array.count -1) {
            
            UIView *line = [UIView new];
            line.tag = 99;
            [stackView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(lastView.mas_bottom).offset(15);
            }];
            
            lastView = line;
        }
        
        UILabel *label = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:textColor];
        label.text = content;
        [stackView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(stackView);
            make.width.lessThanOrEqualTo(stackView);
            if (lastView) {
                
                make.top.equalTo(lastView.mas_bottom).offset(15);
            }else{
                
                make.top.equalTo(stackView);
            }
        }];
        
        lastView = label;
    }
    
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView);
    }];
    
    return stackView;
}

- (UIView *)fourthPartView{
    
    UIView *customView = [UIView new];
    
    YZMCombLabelView *leftFirstView = [YZMCombLabelView new];
    leftFirstView.leftLB.text = @"策略盈亏";
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        [leftFirstView.rightLB setTextColorWithGreatNumber:[self.tradeDetail.profitFreeEx doubleValue] andSmallNumber:0.0];
            leftFirstView.rightLB.text = self.tradeDetail.profitFreeEx.p02fString;
    }else{
        [leftFirstView.rightLB setTextColorWithGreatNumber:self.tradeDetail.profitTotal andSmallNumber:0.0];
        leftFirstView.rightLB.text = @(self.tradeDetail.profitTotal).p02fString;
    }
    
    [customView addSubview:leftFirstView];
    [leftFirstView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(customView).offset(15);
    }];
    
    YZMCombLabelView *leftSecondView = [YZMCombLabelView new];
    leftSecondView.leftLB.text = @"盈利分配";
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        [leftSecondView.rightLB setTextColorWithGreatNumber:[self.tradeDetail.userProfit doubleValue] andSmallNumber:0.0];
        if ([self.tradeDetail.userProfit integerValue] <= 0) {
            leftSecondView.rightLB.text = @"0";
        }else{
            leftSecondView.rightLB.text = self.tradeDetail.userProfit.p02fString;
        }
    }else{
        [leftSecondView.rightLB setTextColorWithGreatNumber:self.tradeDetail.profitShare andSmallNumber:0.0];
        leftSecondView.rightLB.text = @(self.tradeDetail.profitShare).p02fString;
    }
    
    [customView addSubview:leftSecondView];
    [leftSecondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftFirstView);
        make.top.equalTo(leftFirstView.mas_bottom).offset(12);
        make.bottom.equalTo(customView).offset(-15);
    }];
    
    YZMCombLabelView *rightSecondView = [YZMCombLabelView new];
    rightSecondView.leftLB.text = @"亏损赔付";
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        if ([@(0.0) isGreaterThanDouble:[self.tradeDetail.userProfit doubleValue]]) {
            
            leftSecondView.rightLB.text = @"0";
            leftSecondView.rightLB.textColor = [YZMAppStyle colorForLightGrayText];
            rightSecondView.rightLB.text = self.tradeDetail.userProfit.stringValue;
            rightSecondView.rightLB.textColor = [YZMAppStyle colorForDropText];
            
        }else{
            
            rightSecondView.rightLB.text = @"0.00";
            rightSecondView.rightLB.textColor = [YZMAppStyle colorForLightGrayText];
        }
    }else{
        if ([@(0.0) isGreaterThanDouble:self.tradeDetail.profitTotal]) {
            
            leftSecondView.rightLB.text = @"0.00";
            leftSecondView.rightLB.textColor = [YZMAppStyle colorForLightGrayText];
            rightSecondView.rightLB.text = @(self.tradeDetail.profitShare).p02fString;
            rightSecondView.rightLB.textColor = [YZMAppStyle colorForDropText];
            
        }else{
            
            rightSecondView.rightLB.text = @"0.00";
            rightSecondView.rightLB.textColor = [YZMAppStyle colorForLightGrayText];
        }
    }
    
    [customView addSubview:rightSecondView];
    [rightSecondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(customView.mas_centerX);
        make.top.equalTo(leftSecondView);
    }];
    
    return [self unitViewWithString:@"点买人盈亏分配" andCustomView:customView];
}

- (UIView *)fifthPartView{
    
    UIView *customView = [UIView new];
    
    YZMCombLabelView *leftView = [YZMCombLabelView new];
    leftView.leftLB.text = @"冻结";
    leftView.rightLB.textColor = [YZMAppStyle colorForMainTextColor];
    leftView.rightLB.text = @"0";
    [customView addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(customView).offset(15);
        make.bottom.equalTo(customView).offset(-15);
    }];
    
    YZMCombLabelView *middleView = [YZMCombLabelView new];
    middleView.leftLB.text = @"扣减";
    middleView.rightLB.text=@"0";
    middleView.rightLB.textColor = [YZMAppStyle colorForMainTextColor];
    [customView addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(customView);
        make.top.equalTo(leftView);
    }];
    
    YZMCombLabelView *rightView = [YZMCombLabelView new];
    rightView.leftLB.text = @"退回";
    rightView.rightLB.text=@"0";
    rightView.rightLB.textColor = [YZMAppStyle colorForMainTextColor];
    [customView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftView);
        make.right.equalTo(customView).offset(-15);
    }];
    
    switch (self.tradeType) {
        case YZM_TRADE_TYPE_FOR_A_STOCK:
        case YZM_TRADE_TYPE_FOR_A_STOCK_YLB:
        case YZM_TRADE_TYPE_FOR_A_STOCK_ONE:
        case YZM_TRADE_TYPE_FOR_A_STOCK_SIM:{
            double middleLb;
            leftView.rightLB.text = @(self.tradeDetail.lossPrincipal).r2fString;
            
            if (![@(self.tradeDetail.profitShare) isEqualGreaterThanDouble:0]) {
                    middleView.rightLB.text=@(self.tradeDetail.profitShare).r2fString;
                    middleLb=self.tradeDetail.profitShare;
                }else{
                    middleView.rightLB.text=@"0";
                    middleLb=0;
                }
                rightView.rightLB.text=@(self.tradeDetail.lossPrincipal+middleLb).r2fString;
        }
            break;
            
            case YZM_TRADE_TYPE_FOR_A_STOCK_D:
                leftView.rightLB.text = @"0";
                middleView.rightLB.text=@"0";
                rightView.rightLB.text=@"0";
            break;
            
        default:
            break;
    }
    
    return [self unitViewWithString:@"点买人履约保证金结算" andCustomView:customView];
}


- (UIView *)unitViewWithString:(NSString *)title andCustomView:(UIView *)customView{
    
    UIView *view = [UIView new];
    
    UILabel *titleLB = [YZMAppStyle labelWithSmallWhite];
    titleLB.text = title;
    [view addSubview:titleLB];
    [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(view).offset(15);
    }];
    
    customView.backgroundColor = [YZMAppStyle colorForMain];
    [view addSubview:customView];
    [customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLB.mas_bottom).offset(10);
        make.left.right.bottom.equalTo(view);
    }];
    
    return view;
}

#pragma mark - controller manager support

- (void)updateWithParams: (NSDictionary *)params
{
    self.trade = params[@"trade"];
    self.tradeType=[params[@"tradetype"] integerValue];
}

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMStockDetailViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams: params];
    return ctrl;
}

@end
